package net.huashitong.threeSeven.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.core.filter.BaseFilter;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.threeSeven.entity.IndexData;
import net.huashitong.threeSeven.filter.IndexDataFilter;
import net.huashitong.threeSeven.filter.RequestModelFilter;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class IndexDataDao extends StringDao<IndexData> {

    /**
     * 首页
     *
     * @param filter
     * @return
     */
    public List<IndexData> getIndex(IndexDataFilter filter) {
        GenericQuery query = createQuery("obj");
        if (ObjectUtils.isNotBlank(filter.getDateTime())) {
            query.append(" and obj.dateTime = :dateTime").setParam("dateTime", filter.getDateTime());
        }
        if (ObjectUtils.isNotBlank(filter.getIndexCode())) {
            query.append(" and obj.indexCode = :indexCode").setParam("indexCode", filter.getIndexCode());
        }
        query.setOrder("indexCode", BaseFilter.ASC);
        return query.listResult(filter);
    }

    /**
     * 获取最新时间
     *
     * @return
     */
    public String getNewDateTime() {
        GenericQuery query = createQuery("obj");
        query.setOrder("dateTime", BaseFilter.DESC);
        IndexData indexData = query.firstResult();
        return indexData.getDateTime();
    }

    //查询实际完成情况
    //
    public IndexData queryIndexFactDate(RequestModelFilter filter) {
        GenericQuery query = createQuery("obj");

        if (ObjectUtils.isNotBlank(filter.getIndexCode())) {
            query.append(" and obj.indexCode = :indexCode");
            query.setParam("indexCode", filter.getIndexCode());
        }
        if (ObjectUtils.isNotBlank(filter.getDateTime())) {
            query.append(" and obj.dateTime = :dateTime");
            query.setParam("dateTime", filter.getDateTime());
        }
        query.setOrder("indexCode", BaseFilter.ASC);
        List<IndexData> list = query.listResult(filter);
        if (list.size() > 0 && list != null) {
            return list.get(0);
        }
        return null;
    }

}
